<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>英雄列表</title>
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/list.css">
</head>

<body>
    <div class="container">
        <h2>英雄列表</h2>
        <button>点击获取所有 LOL 英雄</button>
        <div class="hero-list">
            
        </div>
    </div>

    <script>
        // 1. 获取元素
        let btn = document.querySelector('button');
        let heroList = document.querySelector('.hero-list')
        // 2. 绑定点击事件
        btn.onclick = function(){
        // 3. 创建xhr对象
        let x = new XMLHttpRequest();
        // 4. 开启一个请求
        x.open('get','http://localhost/getHeros')
        // 5. 发送请求
        x.send()
        // 6. 设置响应体类型
        x.responseType = 'json';
        // 7. 绑定监听
        x.onreadystatechange = function(){
            if(x.readyState === 4){
                if(x.status >= 200 && x.status < 300){
                    let arr = x.response;
                    let str = arr.map(item=> `<div class="hero-item">
                         <img src="${item.avatar}" alt="">
                         <p>${item.name}</p>
                    </div>`).join('');
                    heroList.innerHTML = str;
                }
            }
        }
            // 8. 判断响应状态码
            // 9. 响应完成
                // 10. 遍历获取到的数据
                    // 11. 创建html结构标签
                    // 12. 给对应标签添加属性
                    // 13. 将dom节点添加到hero-list内
        }
        
    </script>
</body>

</html>